package dvsdk.client;
import dvsdk.form.ExUtil;
import dvsdk.form.Util;
import dvsdk.db.BinBase;
import dvsdk.db.BinParam;
import dvsdk.db.GlobalConfig;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Develop
 */
public class BinResult extends BinBase {

    private List<BinRow> rows;
    private String errMsg="";

    public BinResult(List<BinRow> rows) {
        super(GlobalConfig.AGENT_INN+"_"+"0", GlobalConfig.MASTER_INN, GlobalConfig.AGENT_INN, Util.getCurDate(), Util.getCurDate(), "1");
        setRowsParam(rows);
    }
    
    public BinResult(JFrame frame) {
        super(GlobalConfig.AGENT_INN+"_"+"0", GlobalConfig.MASTER_INN, GlobalConfig.AGENT_INN, Util.getCurDate(), Util.getCurDate(), "1");
        setRowsParam(frame);
    }
    
    public boolean test(Component c, JFrame frame) {
        setRowsParam(frame);
        clearRows();
        if (GlobalConfig.DEBUG) System.out.println("F: "+getParamsSQL());
        if (!errMsg.isEmpty()) {
            JOptionPane.showMessageDialog(c, errMsg,"Заполните поля!",JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
        return true;
    }
    
    public void clearRows() {
        errMsg="";
        for (Iterator<BinRow> it = rows.iterator(); it.hasNext();) {
            BinRow string = it.next();
            if (string.getName()==null) continue;
            splitParam(string);
            //string.setName(Util.clearStr(string.getName()));
            //if (Config.DEBUG) System.out.println(string.getName()+"_"+string.getValue()+((string.isTest())?"":"_ERROR!"));
            //allFields+=string.getName()+",";
            if (!string.isTest()) {errMsg+=string.getAlias()+"\n";}
        }
    }
    
    public void clearParam() {
        for (Iterator<BinParam> it = params.iterator(); it.hasNext();) {
            BinParam string = it.next();
            if (string.getName()==null) continue;
            splitParam(string);

            //if (Config.DEBUG) System.out.println(string.getName()+"_"+string.getValue());
            //allFields+=string.getName()+",";
        }
    }
    
    private void splitParam(BinParam string) {
        String type_tmp[]=string.getName().split("\\|");
        if (type_tmp.length==1) string.setName(Util.clearStr(type_tmp[0].trim()));
        if (type_tmp.length==2) { string.setType(type_tmp[0].trim()); string.setName(Util.clearStr(type_tmp[1].trim())); }
    }
    
    private void setRowsParam(JFrame frame){
        this.rows = Util.scanJFrame(frame);
        List<BinParam> bps = new ArrayList();
        bps.addAll(rows);
        this.params=bps;
        clearParam();
    }
    
    private void setRowsParam(List<BinRow> rows) {
        this.rows = rows;
        List<BinParam> bps = new ArrayList();
        bps.addAll(rows);
        this.params=bps;
        clearParam();
    }
    
    public List<BinRow> getRows() {
        return rows;
    }
    
    public void setRows(List<BinRow> rows) {
        setRowsParam(rows);
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }
}